<?php
namespace app\api\controller\new;

use think\Controller;
use think\Request;
use app\common\model\user\User as UserModel;
use think\Db;
use app\common\model\power\Rolepower;

class Agency extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->is_login();
        Rolepower::powerguard('303001'); // 代理商员工管理权限
    }
    private function is_login()
    {
        $data = UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
    // 代理商员工管理（303001）
    public function agencyList(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 303) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $agency_id = Db::name('user_agency')->where('user_id', $loginUser['id'])->value('agency_id');
        if (!$agency_id) {
            return json(['code'=>0, 'msg'=>'未绑定代理商']);
        }
        $user_ids = Db::name('user_agency')->where('agency_id', $agency_id)->column('user_id');
        $keyword = $request->post('keyword');
        $query = User::whereIn('UserId', $user_ids)->where('Role', 304);
        if ($keyword) {
            $query->where('Name', 'like', "%$keyword%");
        }
        $list = $query->field('UserId, Iphone, Name, Sex')->select();
        return json(['code'=>1, 'msg'=>'success', 'data'=>$list]);
    }

    // 代理商员工添加
    public function agencyAdd(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 303) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $agency_id = Db::name('user_agency')->where('user_id', $loginUser['id'])->value('agency_id');
        if (!$agency_id) {
            return json(['code'=>0, 'msg'=>'未绑定代理商']);
        }
        $data = $request->post();
        if (empty($data['Iphone']) || empty($data['Name']) || empty($data['sex'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $user = new User();
        $user->Iphone = $data['Iphone'];
        $user->Name = $data['Name'];
        $user->Sex = $data['sex'];
        $user->Role = 304; // 代理商员工
        $user->State = User::STATEOK;
        $user->save();
        // 关联代理商
        Db::name('user_agency')->insert([
            'user_id' => $user->UserId,
            'agency_id' => $agency_id,
            'create_time' => time()
        ]);
        return json(['code'=>1, 'msg'=>'添加成功']);
    }

    // 代理商员工修改
    public function agencyEdit(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 303) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $data = $request->post();
        if (empty($data['UserId']) || empty($data['Name']) || empty($data['Iphone']) || empty($data['sex'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $user = User::get($data['UserId']);
        if (!$user) {
            return json(['code'=>0, 'msg'=>'用户不存在']);
        }
        $user->Name = $data['Name'];
        $user->Iphone = $data['Iphone'];
        $user->Sex = $data['sex'];
        $user->save();
        return json(['code'=>1, 'msg'=>'修改成功']);
    }

    // 代理商员工删除
    public function agencyDel(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 303) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $userId = $request->post('UserId');
        if (empty($userId)) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        User::destroy($userId);
        Db::name('user_agency')->where('user_id', $userId)->delete();
        return json(['code'=>1, 'msg'=>'删除成功']);
    }
} 